package great.project.param.config.convert;

import com.google.common.collect.ImmutableSet;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.lang.NonNull;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Set;

public class GenericBigDecimalConverter implements GenericConverter {

    @Override
    public Set<ConvertiblePair> getConvertibleTypes() {
        ConvertiblePair[] pairs = new ConvertiblePair[]{
                new ConvertiblePair(Number.class, BigDecimal.class),
                new ConvertiblePair(String.class, BigDecimal.class)};
        return ImmutableSet.copyOf(pairs);
    }

    @Override
    public Object convert(Object source, @NonNull TypeDescriptor sourceType, @NonNull TypeDescriptor targetType) {

        if (sourceType.getType() == BigDecimal.class) {
            return source;
        }

        if(sourceType.getType() == String.class) {
            String number = (String) source;
            return new BigDecimal(number);
        } else {
            Number number = (Number) source;
            BigDecimal converted = BigDecimal.valueOf(number.doubleValue());
            return converted.setScale(2, RoundingMode.HALF_EVEN);
        }

    }

}
